import dao.common.BrandManager;
import dao.common.CategoryManager;
import dao.common.ImageManager;
import dao.common.ProductManager;
import dao.hibernate.BrandManagerHB;
import dao.hibernate.CategoryManagerHB;
import dao.hibernate.ImageManagerHB;
import dao.hibernate.ProductManagerHB;
import models.entities.PaginatedList;
import models.entities.SiteMapPath;
import models.exceptions.CreateEntityException;
import models.shop.*;
import models.shop.path.ProductFilter;
import org.junit.Test;
import play.test.FunctionalTest;

import java.util.ArrayList;
import java.util.List;

public class ProductManager_Tests extends FunctionalTest {


    @Test
    public void create_Test() throws CreateEntityException {
        CategoryManager categoryManager = new CategoryManagerHB();
        Category category = categoryManager.createRoot("Test category for a product");
        ProductManager productManager = new ProductManagerHB();
        Product product1 = getProduct("Test product in root category");
        productManager.save(product1, category.id);
        productManager.clearCache();
        Product restoredProduct = productManager.getById(product1.id, true);
        assertTrue(restoredProduct.id == 1);
        assertTrue(restoredProduct.price == (double) 34000);
        assertTrue(restoredProduct.title.equals("Test product in root category"));
        assertTrue(restoredProduct.category != null);
        assertTrue(restoredProduct.category.title.equals("Test category for a product"));
        Product product2 = addImages(category);
        addBrand(product2.id);
        addSources(product2);
        addSeveralProducts(productManager);
    }

    @Test
    public void getSitePath_Test() {
        ProductManager productManager = new ProductManagerHB();
        ProductFilter filter = getFilter();
        SiteMapPath sitePath = productManager.getSitePath(filter);
        assertTrue(sitePath.sitePaths.size() == 3);
    }

    @Test
    public void getFilteredProductList_Test(){
        ProductFilter filter = getFilter();
        ProductManager productManager = new ProductManagerHB();
        PaginatedList<Product> products = productManager.list(filter);
        assertTrue(products.items.size() > 0);
    }
    private ProductFilter getFilter() {
        ProductFilter filter = new ProductFilter();
        filter.categoryId = 2;
        filter.brandId = 1;
        filter.productId = 1;
        return filter;
    }

    private void addSources(Product product) {
        //ContentSourceManager contentSourceManager = new ContentSourceManagerHB();
        //ContentSource contentSource = contentSourceManager.create("SLAMI", "http://slami.ru");
        product.contentSourceUrls = new ArrayList<ContentSourceUrl>();
    }

    private void addBrand(int productId) {
        ProductManager productManager = new ProductManagerHB();
        Product product = Product.findById(productId);
        ImageManager imageManager = new ImageManagerHB();
        Image img = imageManager.create("/56/56", "456789");
        BrandManager brandManager = new BrandManagerHB();
        product.brand = brandManager.create("Yamaha", img);

        productManager.save(product);
        productManager.clearCache();
        Product product1 = Product.findById(product.id);
        assertTrue(product1.brand != null);
        assertTrue(product1.brand.title.equals("Yamaha"));
        assertTrue(product1.brand.images != null);
        assertTrue(product1.brand.images.get(0).filePath.equals("/56/56"));
    }

    private Product addImages(Category category) throws CreateEntityException {
        CategoryManager categoryManager = new CategoryManagerHB();
        ProductManager productManager = new ProductManagerHB();
        Category subCategory = categoryManager.create("Test subcategory for a product", category.id);
        Product product2 = getProduct("Test product 2");
        product2.category = subCategory;
        product2.images = new ArrayList<Image>();
        Image img1 = new Image("/56/56", "456789");
        Image img2 = new Image("/45/45", "987654");
        ImageManager imageManager = new ImageManagerHB();
        imageManager.save(img1);
        imageManager.save(img2);
        product2.images.add(img1);
        product2.images.add(img2);
        productManager.save(product2);
        productManager.clearCache();
        Product product3 = productManager.getById(product2.id);
        //assertTrue(product3.images.size() == 2);
        return product3;
    }

    private void addSeveralProducts(ProductManager productManager) {
        for (int i = 0; i < 44; i++) {
            Product product = getProduct(String.format("Test product %1$s", i + 2));
            productManager.save(product, "Test subcategory for a product");
        }
    }

    @Test
    public void listItems_Test() {
        ProductManager productManager = new ProductManagerHB();
        int categoryId = 2;
        int pageSize = 20;
        List<Product> productsPage1 = productManager.list(categoryId, 1, pageSize);
        assertTrue(productsPage1.size() == 20);
        List<Product> productsPage3 = productManager.list(categoryId, 3, pageSize);
        assertTrue(productsPage3.size() == 5);
    }

    @Test
    public void makeOrder_Tests() {

    }

    private Product getProduct(String title) {
        Product product = new Product();
        product.title = title;
        product.description = "Test long description";
        product.shortDescription = "Test short description";
        product.price = 34000;
        return product;
    }
}
